<?php

namespace App\Controller\Admin;

use App\Controller\ApiController;
use App\Logic\Admin\AuthLogic;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Logger\LoggerFactory;
use Psr\Http\Message\ResponseInterface;
use Throwable;
/**
 * 添加前缀backend（路由："/backend/方法名"），不添加的话（路由："/文件夹/控制器名/方法名"）
 */
class AuthController extends ApiController
{
    #[Inject]
    protected AuthLogic $authLogic;
    public function __construct(LoggerFactory $loggerFactory)
    {
        parent::__construct($loggerFactory);
        $this->authLogic = new AuthLogic();
    }

    /**
     * Notes:Hyperf 会自动为此方法生成一个 /backend/testFunction 的路由，允许通过 GET 或 POST 方式请求
     * 2025/5/29
     * @return ResponseInterface
     * @throws \Exception
     */
    public function testFunction(): ResponseInterface
    {
        return $this->responseSuccess([]);
    }

    /**
     * Notes:用户登录
     * 2025/5/29
     * @return ResponseInterface
     * @throws \Exception
     */
    public function login(): ResponseInterface
    {
        try {
            $params = $this->request->post();
            $params['ip'] = $this->ip;
            $result = $this->authLogic->login($params);
            return $this->responseSuccess($result);
        }catch (Throwable $throwable){
            return $this->responseError($throwable->getCode(),$throwable->getLine(),$throwable->getFile() ,$throwable->getMessage());
        }
    }
}